
#include <QHBoxLayout>
#include <QPainter>
#include <QPaintEvent>
#include <QPoint>
#include <QRect>
#include <QResizeEvent>
#include <QSignalMapper>
#include <QSize>

#include "ui/button.h"
#include "ui/lobby.h"

Lobby::Lobby(QWidget* pkParent)
    : QWidget(pkParent),
      m_ratio(0.75),
      m_kBackground(":/image/board/board.png"),
      m_kDifficulty(":/image/text/difficulty.png"),
      m_pkOkButton(new Button(":/image/button/normal_ok.png")),
      m_pkEasyButton(new Button(":/image/button/unselected_easy.png", this)),
      m_pkNormalButton(new Button(":/image/button/unselected_normal.png", this)),
      m_pkHardcoreButton(new Button(":/image/button/unselected_hardcore.png", this))
{
    Q_CHECK_PTR(!m_kBackground.isNull());
    Q_CHECK_PTR(!m_kDifficulty.isNull());
    Q_CHECK_PTR(m_pkOkButton);
    Q_CHECK_PTR(m_pkEasyButton);
    Q_CHECK_PTR(m_pkNormalButton);
    Q_CHECK_PTR(m_pkHardcoreButton);

    // Widget setup.
    setAutoFillBackground(false);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    setLayout(new QHBoxLayout(this));
    layout()->setEnabled(false);
    layout()->addWidget(m_pkEasyButton);
    layout()->addWidget(m_pkNormalButton);
    layout()->addWidget(m_pkHardcoreButton);
    layout()->addWidget(m_pkOkButton);

    // Signal mapper setup.
    QSignalMapper* pkSignalMapper = new QSignalMapper(this);
    pkSignalMapper->setMapping(m_pkEasyButton, 0);
    pkSignalMapper->setMapping(m_pkNormalButton, 1);
    pkSignalMapper->setMapping(m_pkHardcoreButton, 2);

    // Connections.
    connect(m_pkEasyButton, SIGNAL(clicked()), pkSignalMapper, SLOT(map()));
    connect(m_pkNormalButton, SIGNAL(clicked()), pkSignalMapper, SLOT(map()));
    connect(m_pkHardcoreButton, SIGNAL(clicked()), pkSignalMapper, SLOT(map()));

    connect(pkSignalMapper, SIGNAL(mapped(int)), this, SIGNAL(difficultySelected(int)));
    connect(m_pkOkButton, SIGNAL(clicked()), this, SIGNAL(start()));
}

Lobby::~Lobby()
{
}

void Lobby::paintEvent(QPaintEvent* pkEvent)
{
    // Propagate.
    QWidget::paintEvent(pkEvent);

    // Base scaling on background pixmap.
    qreal limitingRatio = qMin(width() * m_ratio / m_kBackground.width(),
                               height() * m_ratio / m_kBackground.height());

    // Paint.
    QPainter kPainter(this);

    // Paint the background.
    QSize kScaledBackgroundSize(m_kBackground.width() * limitingRatio, m_kBackground.height() * limitingRatio);
    QPoint kBackgroundOffset((width() - kScaledBackgroundSize.width()) / 2, (height() - kScaledBackgroundSize.height()) / 2 - kScaledBackgroundSize.height() / 8);

    kPainter.drawPixmap(QRectF(kBackgroundOffset.x(), kBackgroundOffset.y(), kScaledBackgroundSize.width(), kScaledBackgroundSize.height()), m_kBackground, m_kBackground.rect());

    // Paint the text.
    QSize kScaledTextSize(m_kDifficulty.width() * limitingRatio, m_kDifficulty.height() * limitingRatio);
    QPoint kTextOffset((width() - kScaledTextSize.width()) / 2, kBackgroundOffset.y() + (kScaledBackgroundSize.height() - kScaledTextSize.height()) / 2 - kScaledTextSize.height() * 2);

    kPainter.drawPixmap(QRectF(kTextOffset.x(), kTextOffset.y(), m_kDifficulty.width() * limitingRatio, m_kDifficulty.height() * limitingRatio), m_kDifficulty, m_kDifficulty.rect());
}

void Lobby::resizeEvent(QResizeEvent* pkEvent)
{
    // Propagate.
    QWidget::resizeEvent(pkEvent);

    // Compute scaled background coordinates.
    qreal limitingRatio = qMin(width() * m_ratio / m_kBackground.width(),
                               height() * m_ratio / m_kBackground.height());
    QSize kScaledSize(m_kBackground.width() * limitingRatio, m_kBackground.height() * limitingRatio);
    QPoint kOffset((width() - kScaledSize.width()) / 2, (height() - kScaledSize.height()) / 2 - kScaledSize.height() / 8);

    // Difficulty button replace.
    QSize kScaledDifficultyButtonSize(m_pkEasyButton->sizeHint().width() * limitingRatio, m_pkEasyButton->sizeHint().height() * limitingRatio);
    QRect kScaledDifficultyButtonGeometry(kOffset.x() + kScaledSize.width() / 2 - kScaledDifficultyButtonSize.width() * 3 / 2,
                                          kOffset.y() + kScaledSize.height() / 2 - kScaledDifficultyButtonSize.height() / 2,
                                          kScaledDifficultyButtonSize.width(),
                                          kScaledDifficultyButtonSize.height());

    m_pkEasyButton->setGeometry(kScaledDifficultyButtonGeometry);
    m_pkNormalButton->setGeometry(kScaledDifficultyButtonGeometry.translated(kScaledDifficultyButtonSize.width(), 0));
    m_pkHardcoreButton->setGeometry(kScaledDifficultyButtonGeometry.translated(kScaledDifficultyButtonSize.width() * 2, 0));

    // Ok button replace.
    int iWidth = m_pkOkButton->sizeHint().width() * limitingRatio;
    int iHeight = m_pkOkButton->sizeHint().height() * limitingRatio;
    int iX = (width() - iWidth) / 2;
    int iY = kOffset.y() + kScaledSize.height() - iHeight / 2;
    m_pkOkButton->setGeometry(iX, iY, iWidth, iHeight);
}
